package com.xm.bluetoothkotlin.util

import android.Manifest
import android.os.Build
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.ToastUtils
import com.xm.bluetoothkotlin.search.BluetoothSearch

/**
 * @创建者：小垚
 * @时间：2023/3/8  14:37
 * @描述：
 */
object PermissionHelp {
    fun sendTiramisuNotific(function: () -> Unit) {
        if (PermissionUtils.isGranted(Manifest.permission.POST_NOTIFICATIONS)) {
            function()
        } else {
            PermissionUtils.permission(Manifest.permission.POST_NOTIFICATIONS)
                .callback(object : PermissionUtils.SimpleCallback {
                    override fun onGranted() {
                        function()
                    }

                    override fun onDenied() {
                        ToastUtils.showShort("不打开权限将有可能在息屏状态下被系统杀死APP的可能")
                    }
                })
                .request()
        }
    }

    fun sendBluetoothConnect(function: () -> Unit) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            if (PermissionUtils.isGranted(Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_ADVERTISE)) {
                function()
            } else {
                PermissionUtils.permission(Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_ADVERTISE)
                    .callback(object : PermissionUtils.SimpleCallback {
                        override fun onGranted() {
                            function()
                        }

                        override fun onDenied() {
                            ToastUtils.showShort("不打开权限开启不了蓝牙")
                        }
                    })
                    .request()
            }
        } else {
            function()
        }
    }

    fun sendBluetoothScan(function: () -> Unit) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            if (PermissionUtils.isGranted(Manifest.permission.BLUETOOTH_SCAN)) {
                function()
            } else {
                PermissionUtils.permission(Manifest.permission.BLUETOOTH_SCAN)
                    .callback(object : PermissionUtils.SimpleCallback {
                        override fun onGranted() {
                            function()
                        }

                        override fun onDenied() {
                            ToastUtils.showShort("不打开权限不能搜索蓝牙设备")
                        }
                    })
                    .request()
            }
        } else {
            if (PermissionUtils.isGranted(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)) {
                function()
            } else {
                PermissionUtils.permission(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
                    .callback(object : PermissionUtils.FullCallback {
                        override fun onGranted(granted: List<String>) {
                            function()
                        }

                        override fun onDenied(deniedForever: List<String>, denied: List<String>) {
                            ToastUtils.showShort("没有此权限，暂不能使用搜索蓝牙功能")
                        }
                    })
                    .request()
            }
        }
    }
}